package net.bwie.userProfile.dwd.log.job;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.userProfile.utils.CDCUtil;
import net.bwie.realtime.userProfile.utils.KafkaUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FlinkCDCOrder {

    public static void main(String[] args) throws  Exception{

        //1.表执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.读取数据源
        DataStreamSource<String> userProfile = CDCUtil.getMysqlSource(env, "user_profile");
//        userProfile.print();

        //3.数据处理
        DataStream<String> resultStream = processLog(userProfile);
        resultStream.print();
        //4. 数据输出
        KafkaUtil.producerKafka(resultStream,"ods_order_detail");


        //5. 执行
        env.execute("FlinkCDCOrder");

    }

    private static DataStream<String> processLog(DataStream<String> stream) {
        // 只需要读取after里面的数据即可

        SingleOutputStreamOperator<String> mapStream = stream.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return JSON.parseObject(value).getJSONObject("after").toJSONString();
            }
        });

        return mapStream;
    }
}
